package com.mz.ozy.activity.me;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.home.ExchangeDetailActivity;
import com.mz.ozy.activity.home.ScheduledDeliveryActivity;
import com.mz.ozy.activity.home.ScheduledDetailActivity;
import com.mz.ozy.activity.shop.FirmOrderActivity1;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.activity.shop.VirtualGoodsActivity;
import com.mz.ozy.adapter.MyOrderInfoAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AssetsBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.MeModel;
import com.mz.ozy.mvp.presenter.MyOrderInfoPresenter;
import com.mz.ozy.mvp.view.MyOrderInfoContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.StringUtils;
import com.mz.ozy.utils.TimeUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.BaseDialog;
import com.mz.ozy.widget.dialog.PayPassDialog;
import com.mz.ozy.widget.dialog.PayPassView;
import com.mz.ozy.widget.dialog.PaySelectDialog;
import com.sobot.chat.ZCSobotApi;
import com.sobot.chat.api.model.Information;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/11/23.
 * 我的订单详情
 */

public class MyOrderInfoActivity extends BaseActivity<MyOrderInfoContract.View, MyOrderInfoPresenter> implements MyOrderInfoContract.View {


    @BindView(R.id.rv_goods)
    RecyclerView mRvGoods;
    @BindView(R.id.nsv_scroll_view)
    NestedScrollView mNsvScrollView;
    @BindView(R.id.rl_logistics)
    RelativeLayout mRlLogistics;
    @BindView(R.id.tv_name_mobile)
    TextView mTvNameMobile;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.tv_order_username)
    TextView mTvOrderUname;
    @BindView(R.id.tv_freight)
    TextView mTvFreight;
    @BindView(R.id.tv_money)
    TextView mTvMoney;
    @BindView(R.id.tv_pay_type)
    TextView mTvPayType;
    @BindView(R.id.tv_order_pay_time)
    TextView mTvOrderPayTime;
    @BindView(R.id.tv_order_over_time)
    TextView mTvOrderOverTime;
    @BindView(R.id.btn_pay)
    Button mBtnPay;
    @BindView(R.id.btn_pinglun)
    Button btnPinglun;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.iv_order_state)
    ImageView ivOrderState;
    @BindView(R.id.btn_cancel)
    Button mBtnCancel;
    @BindView(R.id.btn_delete)
    Button mBtnDelete;
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;
    @BindView(R.id.tv_order_no)
    TextView mTvOrderNo;
    @BindView(R.id.tv_order_state)
    TextView mTvOrderState;

    @BindView(R.id.tv_check_logistics)
    TextView mTvCheckLogistics;
    @BindView(R.id.tv_logistics)
    TextView tv_logistics;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_score)
    TextView tvScore;
    @BindView(R.id.rl_score)
    RelativeLayout mRlScore;
    @BindView(R.id.tv_yh)
    TextView tvYh;
    @BindView(R.id.rl_yh)
    RelativeLayout mRlYh;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.rele_pay_type)
    LinearLayout relePayType;
    @BindView(R.id.tv_creat_time)
    TextView tvCreatTime;
    @BindView(R.id.rele_creat_time)
    LinearLayout releCreatTime;

    @BindView(R.id.rele_payTime)
    LinearLayout relePayTime;
    @BindView(R.id.rele_rela_pay)
    RelativeLayout releRelaPay;

    @BindView(R.id.tv_fh)
    TextView tvFh;
    @BindView(R.id.et_order_note)
    TextView et_order_note;
    @BindView(R.id.rele_fh)
    LinearLayout releFh;
    @BindView(R.id.rele_finish)
    LinearLayout releFinish;
    @BindView(R.id.tv_ps_time)
    TextView tvPsTime;
    @BindView(R.id.rele_ps)
    RelativeLayout relePs;

    @BindView(R.id.customerserviceview)
    ImageView mCustomerServiceView;
    private MyOrderInfoAdapter mAdapter;
    private String tesq;
    private MyOrderInfoBean.OrdermapBean mOrderMap;
    private String ktbalance;
    private String orderID;
    private double totalPrice;
    private double totalMarketPrice;
    private String xfbalance;
    private int state;
    private String user_balance;
    private int type;
    private int beanType;
    private long day;
    private int countDownSec;
    private int countDownSecTime;
    private MeBean mMeBean;
    private MeModel mModel;
    private String mAddress;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                countDownSecTime--;
                if (countDownSecTime > 0) {
                    tvTime.setText(getString(R.string.countdown_msg, Utils.long2DateStr((long) countDownSecTime)));
                    mHandler.sendEmptyMessageDelayed(1, 1000);
                } else {
                    //结束倒计时，刷新当前页面
                    if (mPresenter != null) {
                        mPresenter.initData(MyOrderInfoActivity.this, tesq, beanType);
                    }
                }

            }
        }
    };
    private int payType;
    private PayManager mPayManager;

    public static void start(Context context, String tesq, String orderID, int state, String user_balance, int type, int beanType) {
        Intent starter = new Intent(context, MyOrderInfoActivity.class);
        starter.putExtra("tesq", tesq);
        starter.putExtra("user_balance", user_balance);
        starter.putExtra("orderid", orderID);
        starter.putExtra("state", state);
        starter.putExtra("type", type);
        starter.putExtra("beanType", beanType);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order_info);
        ButterKnife.bind(this);
        tesq = getIntent().getStringExtra("tesq");
        orderID = getIntent().getStringExtra("orderid");
        if (TextUtils.isEmpty(tesq)) {
            ToastUtils.showToast("数据有误");
            return;
        }
        EventBus.getDefault().register(this);
        mPayManager = new PayManager(this);
        initTitle();
        user_balance = getIntent().getStringExtra("user_balance");
        state = getIntent().getIntExtra("state", 0);
        type = getIntent().getIntExtra("type", 0);
        beanType = getIntent().getIntExtra("beanType", 0);
        mPresenter.initData(this, tesq, beanType);
        init();
        //  mPresenter.geAssetsInfo(this);
        getMeInfo(this);
        mCustomerServiceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startActivity(new Intent(MsgActivity.this, CustomerServiceActivity.class));
                Information info = new Information();
                info.setApp_key("f970b1112b8447b6abe76c7acbf450cd");
                info.setPartnerid(Constants.uid);
                info.setUser_nick(mMeBean.getNickname());
                info.setUser_name(mMeBean.getUsername());
                info.setUser_tels(mMeBean.getUsername());
                info.setFace(mMeBean.getUseravatar());
                ZCSobotApi.openZCChat(MyOrderInfoActivity.this, info);
            }
        });
    }

    //获取用户信息，用于进入客服
    private void getMeInfo(Activity activity) {
        mModel = new MeModel();
        mModel.getMeInfo(activity)
                .compose(RxUtil.rxSchedulerHelper(this.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<MeBean>(this) {
                    @Override
                    public void onSuccesss(MeBean bean) {
                        mMeBean = bean;
                    }
                });
    }

    private void init() {
        tvCenterTitle.setText("订单详情");

        mRvGoods.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyOrderInfoAdapter(null, state, type, beanType);
        mRvGoods.setAdapter(mAdapter);
        mRvGoods.setNestedScrollingEnabled(false);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                MyOrderInfoBean.OrdermapBean.ProdcutListBean bean = (MyOrderInfoBean.OrdermapBean.ProdcutListBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.btn_refund:
                        if (bean.getState() == 0) {
                            //startActivity(new Intent(MyOrderInfoActivity.this,ReturnGoodsListActivity.class));
                            if (isOverTime()) {
                                ToastUtils.showToast("商品已超过七天不可退货");
                            } else {
                                ReturnGoodsDetailsActivity.start(MyOrderInfoActivity.this, bean.getPrice(), bean.getMarket_price(), bean.getNum() + "", bean.getId() + "", 1, Constants.TransCode.TransCode10038, -1, -1, null, null, null, null, state,type);
                            }
                        }
                        break;
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyOrderInfoBean.OrdermapBean.ProdcutListBean bean = (MyOrderInfoBean.OrdermapBean.ProdcutListBean) adapter.getData().get(position);
                //商品详情
                if (type == 0) {
                    if (beanType == 0) {
                        Intent intent = new Intent(MyOrderInfoActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", bean.getProduct_id() + "");
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(MyOrderInfoActivity.this, ScheduledDetailActivity.class);
                        intent.putExtra("goodsId", bean.getProduct_id() + "");
                        intent.putExtra("attribute", bean.getArrribute_name());
                        startActivity(intent);
                    }
                } else if (type == 1) {
                  /*  Intent intent = new Intent(MyOrderInfoActivity.this, VirtualGoodsActivity.class);
                    intent.putExtra("goodsId", bean.getProduct_id() + "");
                    startActivity(intent);*/

                    Intent intent = new Intent(MyOrderInfoActivity.this, ShopDetailActivity.class);
                    intent.putExtra("goodsId", bean.getProduct_id() + "");
                    intent.putExtra("tag", Constants.OB_TYPE);
                    startActivity(intent);

                } else {
                    Intent intent = new Intent(MyOrderInfoActivity.this, ExchangeDetailActivity.class);
                    intent.putExtra("goodsId", bean.getProduct_id() + "");
                    startActivity(intent);
                }

            }
        });
    }

    public boolean isOverTime() {
        if (!TextUtils.isEmpty(mOrderMap.getOvertime())) {
            if (TimeUtils.getGapCount(mOrderMap.getOvertime()) > 7.0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public void onInitSuccess(MyOrderInfoBean myOrderInfoBean) {
        mNsvScrollView.setVisibility(View.VISIBLE);
        mCustomerServiceView.setVisibility(View.GONE);
        mOrderMap = myOrderInfoBean.getOrdermap();
        countDownSecTime = TimeUtils.getCountDownTime(myOrderInfoBean.getOrdermap().getEndtime(), myOrderInfoBean.getDate());
        if (countDownSecTime > 0) {  //待确认,倒计时
            tvTime.setVisibility(View.GONE);
            mHandler.sendEmptyMessage(1);
        }
        if (Double.parseDouble(mOrderMap.getMarket_price()) != 0.0) {
            tvScore.setText("-" + mOrderMap.getMarket_price() + "积分");
        } else {
            mRlScore.setVisibility(View.GONE);
//            tvScore.setText("0.00积分");
        }
        if (Double.parseDouble(mOrderMap.getYh_price()) != 0.0) {
            tvYh.setText("-¥" + mOrderMap.getYh_price());
        } else {
            mRlYh.setVisibility(View.GONE);
//            tvYh.setText("¥0.00");
        }
        if (type == 1) {
            tvPrice.setText(mOrderMap.getActual_price() + "欧花");

        } else {
            tvPrice.setText("¥" + mOrderMap.getActual_price());

        }
        tvOrder.setText(mOrderMap.getTesq());
        tvCreatTime.setText(mOrderMap.getCreated());
        tvFh.setText(mOrderMap.getSendtime());
        if (TextUtils.isEmpty(mOrderMap.getSendtime())) {
            releFh.setVisibility(View.GONE);
        }

        et_order_note.setText(TextUtils.isEmpty(mOrderMap.getRemark()) ? "无" : mOrderMap.getRemark());
        mTvOrderNo.setText("订单号:" + mOrderMap.getTesq());
        mTvOrderUname.setText(mOrderMap.getUname());
        mTvNameMobile.setText(mOrderMap.getPhone());
        mAddress = mOrderMap.getReceiver_province()
                + mOrderMap.getReceiver_city()
                + mOrderMap.getReceiver_region()
                + mOrderMap.getReceiver_detail_address();
        mTvAddress.setText(mAddress);
        mTvFreight.setText("¥" + StringUtils.formattingDecimal(mOrderMap.getFreight_amount(), 2));
        //mTvGoodsCount.setText("共" + mOrderMap.getNum() + "件商品 合计");
//        final double totalPrice = Double.parseDouble(mOrderMap.getPrice()) * mOrderMap.getNum();

        //支付时间
        if (!TextUtils.isEmpty(mOrderMap.getPay_time())) {
            mTvOrderPayTime.setText(mOrderMap.getPay_time());
        } else {
            relePayTime.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(mOrderMap.getDelivery_time())) {
            tvPsTime.setText("预计" + Utils.formatDateStr(mOrderMap.getDelivery_time()));
        }
        //完成时间
        if (!TextUtils.isEmpty(mOrderMap.getOvertime())) {
            mTvOrderOverTime.setText(mOrderMap.getOvertime());
        } else {
            releFinish.setVisibility(View.GONE);
        }

//        String picStr = "http://cdn.szfysoft.com/1_05971676157438196.png";
//        MyOrderInfoBean.OrderItemsListBean productBean = new MyOrderInfoBean.OrderItemsListBean(
//                mOrderMap.getNum(), mOrderMap.getPrice(), mOrderMap.getUsername()
//                , picStr, mOrderMap.getTesq()
//        );
//        mOrderMap.getProdcut_list().add(productBean);
        mAdapter.setNewData(mOrderMap.getProdcut_list());


        mAdapter.notifyDataSetChanged();
        time(myOrderInfoBean.getDate());
        totalPrice = 0.00;
        totalMarketPrice = 0.00;
        final List<MyOrderInfoBean.OrdermapBean.ProdcutListBean> prodcut_list = mOrderMap.getProdcut_list();
        for (MyOrderInfoBean.OrdermapBean.ProdcutListBean mapBean : prodcut_list) {
            double price = 0.00;
            if (!TextUtils.isEmpty(mapBean.getPrice())) {
                price = Double.parseDouble(mapBean.getPrice());
            }
            double marketPrice = 0.00;
            if (!TextUtils.isEmpty(mapBean.getPrice())) {
                marketPrice = Double.parseDouble(mapBean.getMarket_price());
            }
            totalPrice = totalPrice + price * mapBean.getNum();
            totalMarketPrice = totalMarketPrice + marketPrice * mapBean.getNum();
        }
        if (type == 1) {
            mTvMoney.setText(getPrice().replaceAll("¥", "") + "欧花");

        } else {
            mTvMoney.setText("¥" + getPrice());

        }
        //mTvExpressDelivery.setText("运费 ¥" + mOrderMap.getFreight_amount());


        //支付方式
        if (type == 2) {
            final double freight = Double.parseDouble(mOrderMap.getFreight_amount());
            if (freight != 0.00) {
                mTvPayType.setText("余额+兑换券");
            } else {
                mTvPayType.setText("兑换券");
            }
        } else {
            if (totalPrice != 0.00 && totalMarketPrice != 0.00) {
                mTvPayType.setText("余额+积分");
            } else if (totalPrice != 0.00) {
                mTvPayType.setText("余额");
            } else if (totalMarketPrice != 0.00) {
                mTvPayType.setText("积分");
            } else {
                mTvPayType.setText("");
            }
        }
        if (mOrderMap.getPaytype() == 0) {
            mTvPayType.setText("欧花");
        } else if (mOrderMap.getPaytype() == 1) {
            mTvPayType.setText("支付宝");
        } else {
            mTvPayType.setText("微信");
        }

        mBtnPay.setVisibility(View.GONE);
        mBtnCancel.setVisibility(View.GONE);
        mBtnConfirm.setVisibility(View.GONE);
        mBtnDelete.setVisibility(View.GONE);
        btnPinglun.setVisibility(View.GONE);
        String orderState = "未知";
        switch (mOrderMap.getState()) {
            case 0:
                orderState = "待支付";
                relePayType.setVisibility(View.GONE);
                releRelaPay.setVisibility(View.GONE);
                relePayTime.setVisibility(View.GONE);
                releFh.setVisibility(View.GONE);
                releFinish.setVisibility(View.GONE);
                tv_logistics.setVisibility(View.GONE);
                mTvOrderState.setText(orderState);
                mTvCheckLogistics.setVisibility(View.GONE);
                mBtnPay.setVisibility(View.VISIBLE);
                mCustomerServiceView.setVisibility(View.VISIBLE);
                break;
            case 1:

                releFh.setVisibility(View.GONE);
                releFinish.setVisibility(View.GONE);
                tv_logistics.setVisibility(View.GONE);
                orderState = "待发货";
                mTvOrderState.setText(orderState);
                mCustomerServiceView.setVisibility(View.VISIBLE);
                break;
            case 2:
                releFinish.setVisibility(View.GONE);
                orderState = "待收货";
                relePs.setVisibility(View.VISIBLE);
                mTvOrderState.setText(orderState);
                mBtnConfirm.setVisibility(View.VISIBLE);
                mCustomerServiceView.setVisibility(View.VISIBLE);
                break;
            case 3:
                tv_logistics.setVisibility(View.GONE);
                orderState = "待退款";
                mTvOrderState.setText(orderState);
                mCustomerServiceView.setVisibility(View.VISIBLE);
                break;
            case 4:
                orderState = "已退款";
                mTvOrderState.setText(orderState);
                //mEtOrderNote.setEnabled(false);
                break;
            case 5:
                orderState = "交易成功";
                //mCustomerServiceView.setVisibility(View.GONE);
                if (mOrderMap.getIsevaluation() == 0) {
                    btnPinglun.setVisibility(View.VISIBLE);
                } else {
                    btnPinglun.setVisibility(View.GONE);
                }
                mTvOrderNo.setVisibility(View.GONE);
                ivOrderState.setImageResource(R.mipmap.icon_order_complete);
                mTvOrderState.setText(orderState);
                //   mEtOrderNote.setEnabled(false);
                break;
            case 6:
                orderState = "已取消";
                mTvOrderState.setText(orderState);
                //mCustomerServiceView.setVisibility(View.GONE);
                //mEtOrderNote.setEnabled(false);
                mTvCheckLogistics.setVisibility(View.GONE);
//                mBtnCancel.setVisibility(View.VISIBLE);
//                mBtnDelete.setVisibility(View.VISIBLE);

                break;
            case 7:
                orderState = "退款失败";
                mTvOrderState.setText(orderState);
                mCustomerServiceView.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void time(String data1) {
        if (!TextUtils.isEmpty(mOrderMap.getOvertime())) {
            countDownSec = TimeUtils.getCountDownTime(mOrderMap.getOvertime(), data1);
            if (countDownSec > 0) {
                long second = countDownSec;
                long minute = 0;
                long hour = 0;
                if (second >= 60) {
                    minute = second / 60;
                    if (minute >= 60) {
                        hour = minute / 60;
                        if (hour > 24) {
                            day = hour / 24;

                        }
                    }
                }

            }
        }
        mAdapter.setTime(day);
    }

    @NotNull
    private String getPrice() {
        String price = "";
        if (totalPrice != 0.00) {
            price += totalPrice;
            price += "";
        }
        if (totalMarketPrice != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += totalMarketPrice;
                price += "积分";
            } else {
                price += totalMarketPrice;
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        return price;
    }

    @NotNull
    public int getPriceType() {
        int type = -1;
        boolean isPrice = false;
        boolean isMarketPrice = false;
        if (totalPrice != 0.00) {
            isPrice = true;
            type = 0;
        }
        if (totalMarketPrice != 0.00) {
            isMarketPrice = true;
            type = 1;
        }
        if (isPrice && isMarketPrice) {
            type = 2;
        }
        return type;
    }

    @Override
    public void onInitError(String msg) {
//        finish();
    }

    @Override
    public void onConfirmSuccess(NetBean netBean) {
        finish();
        ToastUtils.showToast("确认收货成功");
        EventBus.getDefault().post(new EventBean(Constants.EventCode.REFRESH_MY_ORDER, null));
    }

    @Override
    public void onConfirmError(String msg) {

    }

    @Override
    public void onCancelOrderSuccess(NetBean netBean) {
        mPresenter.initData(this, tesq, beanType);
        ToastUtils.showToast("取消订单成功");
        EventBus.getDefault().post(new EventBean(Constants.EventCode.REFRESH_MY_ORDER, null));
    }

    @Override
    public void onCancelOrderError(String msg) {

    }

    @Override
    public void onDelOrderSuccess(NetBean netBean) {
        finish();
        ToastUtils.showToast("删除订单成功");
        EventBus.getDefault().post(new EventBean(Constants.EventCode.REFRESH_MY_ORDER, null));
    }

    @Override
    public void onDelOrderError(String msg) {

    }

    @OnClick({R.id.btn_pinglun, R.id.tv_logistics, R.id.btn_pay, R.id.btn_cancel, R.id.btn_delete, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_logistics://查看物流
                //查看物流显示时跳转到物流详情
//                if (mTvCheckLogistics.getVisibility() != View.GONE) {
//                OrderLogisticsInfoActivity.start(this, tesq, beanType);
                Intent  _LogisticsActivity=new Intent(MyOrderInfoActivity.this, LogisticsActivity.class);
                _LogisticsActivity.putExtra("address",mAddress);
                _LogisticsActivity.putExtra("order",tesq);
                startActivity(_LogisticsActivity);
//                }
                break;
            case R.id.btn_pay:
                showPaySelectDialog();
//                OrderPayActivity.start(this, mOrderID);
                break;
            case R.id.btn_pinglun:
                EvaluateActivity.go(this, mOrderMap.getProdcut_list(), tesq, beanType);
                finish();
//                OrderPayActivity.start(this, mOrderID);
                break;
            case R.id.btn_cancel://取消订单
                final BaseDialog dialog1 = new BaseDialog(this, R.layout.dialog_normal, Gravity.CENTER, true);
                ((TextView) dialog1.getView(R.id.tv_dialog_content)).setText("确认取消订单?");
                dialog1.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog1.toggleDialog();
                    }
                });
                dialog1.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.cancelOrder(MyOrderInfoActivity.this, tesq, beanType);
                        dialog1.toggleDialog();
                    }
                });
                dialog1.toggleDialog();

                break;
            case R.id.btn_delete://删除订单

                final BaseDialog dialog = new BaseDialog(this, R.layout.dialog_normal, Gravity.CENTER, true);
                ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("确认删除吗?");
                dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.toggleDialog();
                    }
                });

                dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //删除订单
                        mPresenter.delOrder(MyOrderInfoActivity.this, tesq);
                        dialog.toggleDialog();
                    }
                });
                dialog.toggleDialog();
                break;
            case R.id.btn_confirm://确认收货
                final BaseDialog dialogConfirm = new BaseDialog(this, R.layout.dialog_normal, Gravity.CENTER, true);
                ((TextView) dialogConfirm.getView(R.id.tv_dialog_content)).setText("请确认您已收到商品");
                ((TextView) dialogConfirm.getView(R.id.tv_dialog_title)).setText("确认收货");
                dialogConfirm.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogConfirm.toggleDialog();
                    }
                });

                dialogConfirm.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.confirm(MyOrderInfoActivity.this, tesq, beanType);
                        dialogConfirm.toggleDialog();
                    }
                });
                dialogConfirm.toggleDialog();

                break;
        }
    }

    @Override
    public void geAssetsInfoSuccess(AssetsBean bean) {
        ktbalance = bean.getKtbalance();
        xfbalance = bean.getXfbalance();
    }

    @Override
    public void onPaySuccess(JSONObject orderBean) {
        if (payType == 0) { //余额支付
            ToastUtils.showToast("支付成功");
            mPresenter.initData(this, tesq, beanType);
        } else if (payType == 2) {  //微信支付
            mPayManager.handleWxPay(orderBean);
        } else if (payType == 1) {  //支付宝支付
            mPayManager.handleAliPay(orderBean.optString("zfb"));
        }else if(payType==3){//积分支付
            ToastUtils.showToast("支付成功");
            mPresenter.initData(this, tesq, beanType);
        }

    }

    @Override
    public void onPayError(String s) {
        ToastUtils.showToast("支付失败");
    }

    private void submitPayOrder(String msg) {
        // mPresenter.onPay(this, orderID, msg);
    }

    /**
     * 支付选择弹窗
     *
     * @param
     */
    private void showPaySelectDialog() {

        if(type == 1){

            final PayPassDialog dialog = new PayPassDialog(this);
      /*  dialog.getPayViewPass().setForgetText(m);
        dialog.getPayViewPass().setYeText(balace1);*/
            dialog.getPayViewPass().setGone();
            dialog.getPayViewPass()
                    .setPayClickListener(new PayPassView.OnPayClickListener() {
                        @Override
                        public void onPassFinish(String passContent) {
                            mPresenter.onPay(MyOrderInfoActivity.this, String.valueOf(mOrderMap.getId()), passContent, 0, mOrderMap.getType(), beanType);
                            dialog.dismiss();
                        }

                        @Override
                        public void onPayClose() {
                            dialog.dismiss();
                            //关闭回调
                        }

                        @Override
                        public void onPayForget() {
                            dialog.dismiss();
                            //点击忘记密码回调
//                        mContext.startActivity(new Intent(mContext, PayPwdActivity.class));
                            EventBus.getDefault().post(new EventBean(Constants.EventCode.JUMP_PAY_PWD, null));

                        }
                    });

        }else {
            PaySelectDialog dialog = new PaySelectDialog(MyOrderInfoActivity.this, 0);
            dialog.setOnDialogListener(new PaySelectDialog.onDialogListener() {
                @Override
                public void onConfirm(int type, String payPwd) {
                    payType = type;
                    mPresenter.onPay(MyOrderInfoActivity.this, String.valueOf(mOrderMap.getId()), payPwd, type, mOrderMap.getType(), beanType);
                }

                @Override
                public void onCancel() {

                }
            });
//        dialog.setData(mList.get(lastPosition).getAmt()+"","");
            dialog.setData(getPrice(mOrderMap), user_balance + "");
            dialog.toggleDialog();
        }

    }

    @NotNull
    private String getPrice(MyOrderInfoBean.OrdermapBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getActual_price()) && Double.parseDouble(item.getActual_price()) != 0.00) {
            price += "";
            price += item.getActual_price();
        }
        if (!TextUtils.isEmpty(item.getMarket_price()) && Double.parseDouble(item.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += item.getMarket_price();
                price += "积分";
            } else {
                price += item.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "￥0.00";
        }
        return price;
    }


//    /**
//     * 检查订单类型
//     *
//     * @param myOrderInfoBean
//     * @return
//     */
//    private int checkType(MyOrderInfoBean myOrderInfoBean) {
//
//        if (myOrderInfoBean == null || myOrderInfoBean.getOrderItemsList() == null || myOrderInfoBean.getOrderItemsList().isEmpty()) {
//            return -1;
//        }
//
//        if (myOrderInfoBean.getOrderItemsList().size() == 1) {
//            if (myOrderInfoBean.getOrderItemsList().get(0).getSalesModuleId() == 100) {
//                return FirmOrderActivity.TYPE_SHOPPING;
//            } else {
//                return FirmOrderActivity.TYPE_EXCHANGE;
//            }
//        }
//
//        int type = myOrderInfoBean.getOrderItemsList().get(0).getSalesModuleId();
//        boolean isSimilar = false;
//        for (int i = 0; i < myOrderInfoBean.getOrderItemsList().size(); i++) {
//            if (type == myOrderInfoBean.getOrderItemsList().get(i).getSalesModuleId()) {
//                isSimilar = true;
//            } else {
//                isSimilar = false;
//            }
//        }
//
//        if (type == 100 && isSimilar) {
//            return FirmOrderActivity.TYPE_SHOPPING;
//        } else if (type == 200 && isSimilar) {
//            return FirmOrderActivity.TYPE_EXCHANGE;
//        } else {
//            return FirmOrderActivity.TYPE_NORMAL;
//        }
//    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        //刷新购物车
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)) {
            mPresenter.initData(this, tesq, beanType);
        }
        if ((event.getCode() == Constants.EventCode.JUMP_PAY_PWD)) {
            startActivity(new Intent(this, PayPwdActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
